<?php

namespace App\Http\Controllers;


use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;


class NewsController extends Controller
{
    public function lists(Request $request){
        $category_id = $request->input('category_id');
        if (empty($category_id)){
            return response()->json(['code'=>10001,'msg'=>'分类不存在']);
        }

        $lists = DB::table('news')->where('category_id',$category_id)->orderBy('is_recomend','desc')->orderBy('id','desc')->paginate(8);
        $lists =  objectToArray($lists);
        foreach ($lists['data'] as $k=>$v){
            $lists['data'][$k]['image'] = config('app.url').'/upload/'.$v['image'];

        }
        return response()->json(['code'=>0,'data'=>$lists]);

    }

    public function welfare_lists(){


        $lists = DB::table('welfare')->orderBy('id','desc')->paginate(8);
        $lists =  objectToArray($lists);

        return response()->json(['code'=>0,'data'=>$lists]);

    }

    public function welfare_info(Request $request){

        $id = $request->input('id');
        $data = DB::table('welfare')->where('id',$id)->first();
        $data =  objectToArray($data);

        return response()->json(['code'=>0,'data'=>$data]);

    }


    public function detail(Request $request){
        $news_id = $request->input('news_id',0);
        $data = DB::table('news')->where('id',$news_id)->first();
        if (empty($data)){
            return response()->json(['code'=>10002,'msg'=>'文章不存在']);
        }
        $data =  objectToArray($data);
        $data['image'] = config('app.url').'/upload/'.$data['image'];
        if (!empty($data['all_num'])){
            $data['per_num'] = number_format((($data['pro_num']/$data['all_num'])*100),0).'%';
        }else{
            $data['per_num'] = $data['all_num'];
        }

        return response()->json(['code'=>0,'data'=>$data]);

    }

    public function addNum(Request $request){

        $news_id = $request->input('news_id',0);
        $user_id = $request->input('user_id',0);
        $data = DB::table('news')->where('id',$news_id)->first();
        if (empty($data)){
            return response()->json(['code'=>10002,'msg'=>'文章不存在']);
        }
        $data =  objectToArray($data);
        $now = date('Y-m-d',time());
        $res = DB::table('news_like')->whereDate('created_at', $now)->where('news_id',$news_id)->where('user_id',$user_id)->first();
        if (!empty($res)){
            return response()->json(['code'=>10004,'msg'=>'当日只能操作一次']);
        }

        $temp = [
            'news_id'=>$news_id,
            'user_id'=>$user_id,
            'one_num'=>$data['one_num'],
            'created_at'=>date('Y-m-d H:i:s'),
        ];
        $res = DB::table('news_like')->insertGetId($temp);
        $temp['id'] = $res;

        return response()->json(['code'=>0,'data'=>$temp]);

    }

    //文章关注
    public function like(Request $request){
        $news_id = $request->input('news_id',0);
        $user_id = $request->input('user_id',0);
    }


}